/*
 * Part of the ACID Project
 * Developed in 2011 by Morgan Taschuk
 */

/*
 * GetAnalysisFiles.java
 *
 * Created on 12-May-2011, 17:24:46
 */
package acid.parse;

import acid.spring.ExperimentAnalysisService;
import acid.spring.ExperimentService;
import acid.spring.OrganismService;
import acid.spring.TissueService;
import acid.spring.db.Experiment;
import acid.spring.db.ExperimentAnalysis;
import acid.spring.db.Organism;
import acid.spring.db.Tissue;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author morgan
 */
public class GetAnalysisFilesPanel extends javax.swing.JPanel
{

    private TissueService tissueService;
    private ExperimentService experimentService;
    private ExperimentAnalysisService analysisService;
    private OrganismService organismService;

    /** Creates new form GetAnalysisFiles */
    public GetAnalysisFilesPanel()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "classpath:/jdbc-services.xml");
        BeanFactory factory = context;
        tissueService = (TissueService) factory.getBean(TissueService.class);
        experimentService = (ExperimentService) factory.getBean(ExperimentService.class);
        analysisService = (ExperimentAnalysisService) factory.getBean(ExperimentAnalysisService.class);
        organismService = (OrganismService)factory.getBean(OrganismService.class);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        browseButton = new javax.swing.JButton();
        fileTextField = new javax.swing.JTextField();
        tissueBox = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        experimentBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        analysisBox = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        organismBox = new javax.swing.JComboBox();

        jLabel1.setText("Select Analysis File:");

        browseButton.setText("Browse...");
        browseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButtonActionPerformed(evt);
            }
        });

        fileTextField.setEditable(false);

        List<Tissue> tissueList = tissueService.listAllTissues();
        tissueBox.setModel(new javax.swing.DefaultComboBoxModel(tissueList.toArray()));

        jLabel2.setText("Select Experiment:");

        jLabel3.setText("Select Analysis:");

        List<Experiment> expList = experimentService.listAllExperiments();
        experimentBox.setModel(new javax.swing.DefaultComboBoxModel(expList.toArray()));
        experimentBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                experimentBoxActionPerformed(evt);
            }
        });

        jLabel4.setText("Select Tissue:");

        analysisBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "a" }));
        analysisBox.setEnabled(false);

        jLabel5.setText("Select Organism:");

        List<Organism> orgList = organismService.listAllOrganisms();
        organismBox.setModel(new javax.swing.DefaultComboBoxModel(orgList.toArray()));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tissueBox, 0, 344, Short.MAX_VALUE)
                    .addComponent(organismBox, 0, 344, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(fileTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(browseButton))
                    .addComponent(analysisBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, 344, Short.MAX_VALUE)
                    .addComponent(experimentBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, 344, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(browseButton)
                    .addComponent(fileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(organismBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(experimentBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(analysisBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tissueBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void experimentBoxActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_experimentBoxActionPerformed
    {//GEN-HEADEREND:event_experimentBoxActionPerformed
        Experiment experiment = (Experiment) experimentBox.getSelectedItem();
        List<ExperimentAnalysis> list = analysisService.getExperimentAnalysisByExperiment(experiment);
        DefaultComboBoxModel model = (DefaultComboBoxModel)analysisBox.getModel();
        model.removeAllElements();
        for (ExperimentAnalysis ea : list)
        {
            model.addElement(ea);
        }
        analysisBox.setEnabled(true);
    }//GEN-LAST:event_experimentBoxActionPerformed

    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_browseButtonActionPerformed
    {//GEN-HEADEREND:event_browseButtonActionPerformed
        JFileChooser chooser = new JFileChooser("/home/morgan/GEO/");
        int i = chooser.showOpenDialog(this);
        if (i == JFileChooser.APPROVE_OPTION)
        {
            fileTextField.setText(chooser.getSelectedFile().getAbsolutePath());
        }

    }

    public JComboBox getAnalysisBox()
    {
        return analysisBox;
    }

    public JTextField getFileTextField()
    {
        return fileTextField;
    }

    public JComboBox getTissueBox()
    {
        return tissueBox;
        
    }//GEN-LAST:event_browseButtonActionPerformed

    public JComboBox getOrganismBox()
    {
        return organismBox;
    }

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox analysisBox;
    private javax.swing.JButton browseButton;
    private javax.swing.JComboBox experimentBox;
    private javax.swing.JTextField fileTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JComboBox organismBox;
    private javax.swing.JComboBox tissueBox;
    // End of variables declaration//GEN-END:variables
}
